﻿using System.Collections;
using System.Configuration;
using System.Text;
using MPMovieCatalog.Configuration.Sections;
using MPMovieCatalog.Util;

namespace MPMovieCatalog.Configuration
{
    public class ConfigurationWrapper
    {
        private System.Configuration.Configuration m_configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        public ConnectionSection ConnectionSection
        {
            get
            {
                ConnectionSection section = (ConnectionSection)m_configuration.Sections["connection"];
                if (section == null)
                {
                    section = new ConnectionSection();
                    section.Host = "localhost";
                    section.Port = 8080;
                    m_configuration.Sections.Add("connection", section);
                }
                return section;
            }
        }

        public ScriptSection ScriptSection
        {
            get
            {
                ScriptSection section = (ScriptSection)m_configuration.Sections["scripts"];
                if (section == null)
                {
                    section = new ScriptSection();
                    section.Scripts = new ArrayList();
                    m_configuration.Sections.Add("scripts", section);
                }
                return section;
            }
        }

        public GUISection GUISection
        {
            get
            {
                GUISection section = (GUISection)m_configuration.Sections["interface"];
                if (section == null)
                {
                    section = new GUISection();
                    section.Template = "view_item_details_aqua2";
                    m_configuration.Sections.Add("interface", section);
                }

                if (section.Columns.Count == 0)
                {
                    ArrayList columns = new ArrayList();
                    columns.Add(new Column(Fields.Title, 0));
                    section.Columns = columns;
                }

                return section;
            }
        }

        public PrintSection PrintSection
        {
            get
            {
                PrintSection section = (PrintSection)m_configuration.Sections["print"];
                if (section == null)
                {
                    section = new PrintSection();
                    section.FontName = "Arial";
                    section.FontSize = 8;
                    section.FontColor = "Black";

                    ArrayList fields = new ArrayList();
                    fields.Add(Fields.Title);
                    fields.Add(Fields.Director);
                    fields.Add(Fields.Year);
                    fields.Add(Fields.Genres);

                    section.Fields = fields;
                    m_configuration.Sections.Add("print", section);
                }
                return section;
            }
        }

        public TextExportSection TextExportSection
        {
            get
            {
                TextExportSection section = (TextExportSection)m_configuration.Sections["textExport"];
                if (section == null)
                {
                    section = new TextExportSection();
                    section.Encoding = Encoding.Default.WebName;
                    section.Movies = 1;
                    section.Delimeter = 0;
                    section.Qualifier = 0;
                    section.LineBreaks = 0;

                    ArrayList fields = new ArrayList();
                    fields.Add(Fields.Title);
                    fields.Add(Fields.Director);
                    fields.Add(Fields.Year);
                    fields.Add(Fields.Genres);

                    section.Fields = fields;
                    m_configuration.Sections.Add("textExport", section);
                }
                return section;
            }
        }

        public AMCExportSection AMCExportSection
        {
            get
            {
                AMCExportSection section = (AMCExportSection)m_configuration.Sections["amcExport"];
                if (section == null)
                {
                    section = new AMCExportSection();
                    section.Encoding = Encoding.Default.WebName;
                    section.Movies = 1;
                    m_configuration.Sections.Add("amcExport", section);
                }
                return section;
            }
        }

        public HTMLExportSection HTMLExportSection
        {
            get
            {
                HTMLExportSection section = (HTMLExportSection)m_configuration.Sections["htmlExport"];
                if (section == null)
                {
                    section = new HTMLExportSection();
                    section.Movies = 0;
                    section.ListType = 0;
                    section.ItemsPerPage = 12;
                    section.IndexFileName = "index.html";
                    section.OverwriteFiles = true;
                    section.OpenIndexPage = true;

                    section.IndexTtitle = "Movie Collection";
                    section.IndexType = 1;

                    section.FontName = "Arial";
                    section.FontSize = 8;
                    section.FontColor = "White";
                    section.BackColor = "#046688";
                    section.ThumbSize = 200;
                    section.IncludeCreationDate = true;
                    section.CustomLayoutType = 0;
                    section.ShadingEnabled = true;
                    section.ShowThumb = true;

                    ArrayList fields = new ArrayList();
                    fields.Add(Fields.Title);
                    fields.Add(Fields.Director);
                    fields.Add(Fields.Year);
                    fields.Add(Fields.Genres);

                    section.Fields = fields;

                    section.ColumnsCount = 3;
                    section.ThumbBackColor = "#777777";
                    section.ShowDescription = false;

                    section.IndexTemplate = "exportindex_item_aqua2";
                    section.CreateDetails = true;
                    section.DetailsTemplate = "exportdetails_item_aqua2";

                    m_configuration.Sections.Add("htmlExport", section);
                }

                return section;
            }
        }

        public void SaveConfiguration()
        {
            m_configuration.Save();
        }
    }
}
